package com.qcby.service.impl;

import com.qcby.dao.ScreeningOrganizeMapper;
import com.qcby.model.ScreeningOrganize;
import com.qcby.model.dto.OrganizeVo;
import com.qcby.model.dto.ScreeningOrganizeQueryDto;
import com.qcby.model.dto.ScreeningOrganizeQueryDto2;
import com.qcby.query.ScreeningOrganizeQuery;
import com.qcby.service.ScreeningOrganizeService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ScreeningOrganizeServiceImpl extends BaseServiceImpl<ScreeningOrganizeQuery, ScreeningOrganize> implements
      ScreeningOrganizeService {


   private ScreeningOrganizeMapper screeningOrganizeDao;

   @Autowired
   public void setBrandDao(ScreeningOrganizeMapper screeningOrganizeDao) {
      this.screeningOrganizeDao = screeningOrganizeDao;
      this.baseDao = screeningOrganizeDao;
   }

   @Override
   public Page<OrganizeVo> selectCondition(ScreeningOrganizeQueryDto queryDto) {
      Integer pageNo = queryDto.getPageNo();
      Integer pageSize = queryDto.getPageSize();
      Integer startNum = (pageNo - 1) * pageSize;
      Page<OrganizeVo> page = new Page<>();
      page.setPageNo(pageNo);
      page.setPageSize(pageSize);
      page.setStartNum(startNum);

      Integer cid = queryDto.getCid();
      Integer mid = queryDto.getMid();
      Integer hid = queryDto.getHid();
      String day = queryDto.getDay();
      Timestamp now = new Timestamp(System.currentTimeMillis());

      List<OrganizeVo> screeningOrganizeList = screeningOrganizeDao.selectCondition(cid, hid, mid, day, now, startNum, pageSize);
      Integer count = screeningOrganizeDao.selectConditionCount(cid, hid, mid, day, now);


      //把总记录数设置给page对象
      page.setTotalCount(count);
      page.setList(screeningOrganizeList);
      page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
      return page;
   }

   @Override
   public Page<OrganizeVo> selectCondition2(ScreeningOrganizeQueryDto2 queryDto2) {
      Integer pageNo = queryDto2.getPageNo();
      Integer pageSize = queryDto2.getPageSize();
      Integer startNum = (pageNo - 1) * pageSize;
      Page<OrganizeVo> page = new Page<>();
      page.setPageNo(pageNo);
      page.setPageSize(pageSize);
      page.setStartNum(startNum);

      Integer cid = queryDto2.getCid();
      Integer mid = queryDto2.getMid();
      Integer hid = queryDto2.getHid();

      List<OrganizeVo> screeningOrganizeList = screeningOrganizeDao.selectCondition2(cid, hid, mid, startNum, pageSize);
      Integer count = screeningOrganizeDao.selectConditionCount2(cid, hid, mid);


      //把总记录数设置给page对象
      page.setTotalCount(count);
      page.setList(screeningOrganizeList);
      page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
      return page;
   }

   @Override
   public OrganizeVo selectById(Integer soId) {

      return screeningOrganizeDao.selectById(soId);
   }

   @Override
   public void updateLeftSeat(Integer soId) {
      screeningOrganizeDao.updateLeftSeat(soId);
   }
}
